﻿using IPC.DbMapper;
using IPC.DbMapper.IPCDBConnect;
using IPC.LogCommon.DelegateAndEvent;
using IPC.LogCommon.log4net;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace IPC.WinForm.Bussiness
{
    public class DbBussiness
    {
        #region 单例模式
        private static DbBussiness _instance;
        public static DbBussiness Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DbBussiness();
                }
                return _instance;
            }
        }
        #endregion

        #region 增加通道
        public void AddChannel(string channelName, string channelType,string guid)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                iPCDbContext.Channels.Add(new DbMapper.Channel() { Guid= guid, ChannelName = channelName, ChannelType = channelType });
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"添加通道{channelName}失败 {ex.Message}");
            }
        }
        #endregion

        #region 删除通道
        public void RemoveChannel(string guid)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                RemoveEquipByChannel(guid);
                iPCDbContext.Channels.Where(o => o.Guid == guid).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除通道{guid}失败 {ex.Message}");
            }
        }

        /// <summary>
        /// 删除通道下所有设备
        /// </summary>
        /// <param name="channelName"></param>
        private void RemoveEquipByChannel(string channelName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                List<DbMapper.Equip> equips = iPCDbContext.Equips.Where(o => o.ChannelName == channelName).ToList();
                foreach (var item in equips)
                {
                    RemoveEquip(item.EquipFullName);
                }
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除通道下所有设备{channelName}失败 {ex.Message}");
            }
        }
        #endregion

        #region 新建设备
        public void AddEquip(string ip,string port,string slot,string rack,string type,string name,string fullName,string channelName, string guid,string channelType)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                iPCDbContext.Equips.Add(new DbMapper.Equip()
                {
                    Guid = guid,
                    Ip = ip,
                    Port = port,
                    Slot = slot,
                    Rack = rack,
                    Type = type,
                    Name = name,
                    EquipFullName = fullName,
                    ChannelName = channelName,
                    ChannelType = channelType
                });
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"新建设备{name}失败 {ex.Message}");
            }
        }
        #endregion

        #region 删除设备
        public void RemoveEquip(string equipFullName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                RemoveTagsByEquip(equipFullName);
                RemoveGroupsByEquip(equipFullName);
                iPCDbContext.Equips.Where(o => o.EquipFullName == equipFullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除设备{equipFullName}失败 {ex.Message}");
            }
        }

        /// <summary>
        /// 删除设备下所有标签
        /// </summary>
        /// <param name="equipFullName"></param>
        private void RemoveTagsByEquip(string equipFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                iPCDbContext.Tags.Where(o => o.EquipFullName == equipFullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除设备下所有标签{equipFullName}失败 {ex.Message}");
            }
        }

        /// <summary>
        /// 删除设备下所有组
        /// </summary>
        /// <param name="equipFullName"></param>
        private void RemoveGroupsByEquip(string equipFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Group> groups = iPCDbContext.Groups.Where(o => o.EquipFullName == equipFullName).ToList();
                foreach (var item in groups)
                {
                    RemoveTagsByGroup(item.GroupFullName);
                }
                iPCDbContext.Groups.Where(o => o.EquipFullName == equipFullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除设备下所有组{equipFullName}失败 {ex.Message}");
            }
        }

        #endregion

        #region 新建组
        public void AddGroup(string name,string groupFullName,string equipFullName, string guid)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                iPCDbContext.Groups.Add(new DbMapper.Group()
                {
                    Guid = guid,
                    Name = name,
                    GroupFullName = groupFullName,
                    EquipFullName = equipFullName
                });
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"新建组{name}失败 {ex.Message}");
            }
        }
        #endregion

        #region 删除组
        public void RemoveGroup(string groupFullName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                RemoveTagsByGroup(groupFullName);
                iPCDbContext.Groups.Where(o => o.GroupFullName == groupFullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除组{groupFullName}失败 {ex.Message}");
            }
        }

        /// <summary>
        /// 删除组下所有标签
        /// </summary>
        /// <param name="groupFullName"></param>
        private void RemoveTagsByGroup(string groupFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                iPCDbContext.Tags.Where(o => o.GroupFullName == groupFullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除组下所有标签{groupFullName}失败 {ex.Message}");
            }
        }

        #endregion

        #region 新建标签
        public void AddTag(string name,string address,string dataType,int frequency,double zoom,string remark,string value,string readWrite
            ,string fullName,string groupFullName,string equipFullName, string guid)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                iPCDbContext.Tags.Add(new DbMapper.Tag()
                {
                    Guid = guid,
                    Name=name,
                    Address=address,
                    DataType=dataType,
                    Frequency=frequency,
                    Zoom=zoom,
                    Remark = remark,
                    Value=value,
                    ReadWrite=readWrite,
                    FullName=fullName,
                    GroupFullName=groupFullName,
                    EquipFullName=equipFullName
                });
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"新建标签{name}失败 {ex.Message}");
            }
        }
        #endregion

        #region 删除标签
        public void RemoveTag(string fullName)
        {
            try
            {
                IPCDbContext iPCDbContext = new IPCDbContext();
                iPCDbContext.Tags.Where(o => o.FullName == fullName).ExecuteDeleteAsync();
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"删除组{fullName}失败 {ex.Message}");
            }
        }
        #endregion

        #region 查询所有通道
        /// <summary>
        /// 查询所有通道
        /// </summary>
        /// <returns></returns>
        public List<DbMapper.Channel> GetChannels()
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Channel> channels = iPCDbContext.Channels.ToList();
                iPCDbContext.Dispose();
                return channels;
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"初始化配置失败 {ex.Message}");
                return new List<DbMapper.Channel>();
            }
        }
        #endregion

        #region 查询通道下所有设备
        /// <summary>
        /// 查询通道下所有设备
        /// </summary>
        /// <param name="channelName">通道名</param>
        /// <returns></returns>
        public List<DbMapper.Equip> GetEquipsByChannel(string channelName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Equip> equips = iPCDbContext.Equips.Where(o=>o.ChannelName == channelName).ToList();
                iPCDbContext.Dispose();
                return equips;
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"初始化{channelName}设备配置失败 {ex.Message}");
                return new List<DbMapper.Equip>();
            }
        }
        #endregion

        #region 查询设备下所有组
        /// <summary>
        /// 查询设备下所有组
        /// </summary>
        /// <param name="equipFullName">设备全名</param>
        /// <returns></returns>
        public List<DbMapper.Group> GetGroupsByEquip(string equipFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Group> groups = iPCDbContext.Groups.Where(o => o.EquipFullName == equipFullName).ToList();
                iPCDbContext.Dispose();
                return groups;
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"初始化{equipFullName}组配置失败 {ex.Message}");
                return new List<DbMapper.Group>();
            }
        }
        #endregion

        #region 查询设备下所有标签
        /// <summary>
        /// 查询设备下所有组
        /// </summary>
        /// <param name="equipFullName">设备全名</param>
        /// <returns></returns>
        public List<DbMapper.Tag> GetTagsByEquip(string equipFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Tag> tags = iPCDbContext.Tags.Where(o => o.EquipFullName == equipFullName).ToList();
                iPCDbContext.Dispose();
                return tags;
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"初始化{equipFullName}标签配置失败 {ex.Message}");
                return new List<DbMapper.Tag>();
            }
        }
        #endregion

        #region 查询组下所有标签
        /// <summary>
        /// 查询设备下所有组
        /// </summary>
        /// <param name="equipFullName">设备全名</param>
        /// <returns></returns>
        public List<DbMapper.Tag> GetTagsByGroup(string groupFullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                List<DbMapper.Tag> tags = iPCDbContext.Tags.Where(o => o.GroupFullName == groupFullName).ToList();
                iPCDbContext.Dispose();
                return tags;
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"初始化{groupFullName}标签配置失败 {ex.Message}");
                return new List<DbMapper.Tag>();
            }
        }
        #endregion

        #region 更新设备
        public void UpdateEquip(string ip, string equipFullName,string port,string slot,string rack,string type,string name)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                DbMapper.Equip equip = iPCDbContext.Equips.Where(o => o.EquipFullName == equipFullName).FirstOrDefault();
                equip.Ip = ip;
                equip.Port = port;
                equip.Slot = slot;
                equip.Rack = rack;
                equip.Type = type;
                equip.Name = name;
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch(Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"修改设备{equipFullName}失败 {ex.Message}");
            }
        }
        #endregion

        #region 更新标签
        public void UpdateTag(string guid,string name, string fullName, string remark, string address, string dataType, string readWrite,int frequency,double zoom)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                DbMapper.Tag tag = iPCDbContext.Tags.Where(o => o.FullName == fullName).FirstOrDefault();
                tag.Remark = remark;
                tag.Address = address;
                tag.DataType = dataType;
                tag.ReadWrite = readWrite;
                tag.Frequency = frequency;
                tag.Zoom = zoom;
                tag.Name = name;
                iPCDbContext.SaveChangesAsync();
                iPCDbContext.Dispose();
            }
            catch (Exception ex)
            {
                DelegateAndEvent.Instance.AddAlarmInfo($"修改标签{fullName}失败 {ex.Message}");
            }
        }
        #endregion

        #region 根据fullname获取标签
        public Tag GetTag(string fullName)
        {
            try
            {
                var iPCDbContext = new IPCDbContext();
                Tag tag = iPCDbContext.Tags.Where(o => o.FullName == fullName).First();
                if (tag != null)
                {
                    return tag;
                }
                return new Tag();
            }
            catch (Exception ex)
            {
                LogClass.Instance.Error($"根据fullname获取标签{fullName}错误{ex.Message}");
                return new Tag();
            }
        }
        #endregion
    }
}
